package security.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import main.Service;
import security.Licence;
import security.dao.SecurityDAOInterface;
import user.User;

public class SecurityDAOJPAImpl implements SecurityDAOInterface {
	
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("CAS-Security");
	EntityManager em = emf.createEntityManager();
	EntityTransaction tx = em.getTransaction();
	
	@Override
	public void addLicence(Licence licence) {
		try{
			tx.begin();
			em.persist(licence);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public void bindServiceAndLicence(Licence licence, Service service) {
		try{
			tx.begin();
			licence.addService(service);
			service.addLicence(licence);
			em.merge(licence);
			em.merge(service);//?
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public void addService(Service service) {
		try{
			tx.begin();
			em.persist(service);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}
	
	@Override
	public Licence getLicenceByUserID(String userId) {
		Licence licence = null;
		try{
			tx.begin();
			Query query = em.createQuery("SELECT u.licenceId FROM User u WHERE u.userId =:userid");
			query.setParameter("userid", userId);
			licence = (Licence) query.getSingleResult();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
		return licence;
	}
	
	@Override
	public Licence getLicenceByLicenceID(String licenceId) {
		Licence licence = null;
		try{
			tx.begin();
			Query query = em.createQuery("SELECT l FROM Licence l WHERE l.licenceId =:id");
			query.setParameter("id", licenceId);
			licence = (Licence) query.getSingleResult();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
		return licence;
	}
	
	@Override
	public List<Licence> getLicences() {
		List<Licence> licenceList;
		try{
			tx.begin();
			Query query = em.createQuery("SELECT l FROM Licence l");
			licenceList = query.getResultList();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			return null;
		}
		return licenceList;
	}

	@Override
	public List<String> getServicesListByUserID(String userId) {
		try{
			tx.begin();
			Query query = em.createQuery("");
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
		return null;
	}

	@Override
	public void modifyLicence(Licence licence) {
		try{
			tx.begin();
			em.merge(licence);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public void modifyService(String userId, Service service) {
		try{
			tx.begin();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public void removeLicence(String licenceId) {
		try{
			tx.begin();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public void removeService(String userId, Service service) {
		try{
			tx.begin();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}
	
	@Override
	public User findLicenceByUser(String userid) {
		return null;
	}
	@Override
	public Licence findUserByLicence(String licenceId) {
		return null;
	}

}
